package com.road.yishi.user;

import org.apache.mina.core.session.IoSession;

import com.road.yishi.mina.LinkedClient;
import com.road.yishi.proto.PBMessage;

/**
 * @Description :玩家对象
 * @FileName: User.java
 * @Author :WeiHui.Zhang
 * @Data : 2016年2月26日 下午12:13:01
 * @Version:V1.00
 */
public class User {

	private final int userId;
	private IoSession session;
	private final Object obj;
	private long lastSyncTime;
	private int acceleratCount;
	private LinkedClient crossClient;
	private String serverName;

	public User(final int userId, IoSession session) {
		this.userId = userId;
		this.session = session;
		if (session != null) {
			this.session.setAttribute("userId", userId);
		}
		obj = new Object();
		lastSyncTime = 0;
		acceleratCount = 0;
	}

	public int getUserId() {
		return userId;
	}

	public void setSession(IoSession session) {
		this.session = session;
	}

	public IoSession getSession() {
		return session;
	}

	public long getLastSyncTime() {
		return lastSyncTime;
	}

	public void setLastSyncTime(long lastSyncTime) {
		this.lastSyncTime = lastSyncTime;
	}

	public int getAcceleratCount() {
		return acceleratCount;
	}

	public void setAcceleratCount(int acceleratCount) {
		this.acceleratCount = acceleratCount;
	}

	public void addAcceleratCount() {
		acceleratCount++;
	}

	public LinkedClient getCrossClient() {
		return crossClient;
	}

	public void setCrossClient(LinkedClient crossClient) {
		this.crossClient = crossClient;
	}

	public void sendToUser(PBMessage packet) {
		if (session != null && !session.isClosing()) {
			// 线程同步发送
			synchronized (obj) {
				session.write(packet);
			}
		}
	}

	public String getServerName() {
		return serverName;
	}

	public void setServerName(String serverName) {
		this.serverName = serverName;
	}
}
